package com.moss.web;


import com.moss.common.constant.Constants;
import com.moss.common.core.BaseController;
import com.moss.common.domain.AjaxResult;

import com.moss.common.domain.TreeEntity;
import com.moss.common.entity.SysDeptEntity;
import com.moss.common.entity.SysRoleEntity;
import com.moss.common.entity.SysUserEntity;
import com.moss.common.util.IdUtil;
import com.moss.common.util.TokenUtil;
import com.moss.system.service.DeptService;
import com.moss.system.service.PostService;
import com.moss.system.service.RoleService;
import com.moss.system.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {


    @Resource
    UserService userService;

    @Resource
    DeptService deptService;

    @Resource
    RoleService roleService;


    @GetMapping("/deptTree")
    public AjaxResult<List<TreeEntity>> getDeptTree(SysDeptEntity dept){
        List<TreeEntity> deptTree = deptService.getDeptTree(dept);
        return AjaxResult.success(deptTree);
    }

    @GetMapping("/list")
    public AjaxResult<List<SysUserEntity>> list(SysUserEntity user){
        if (user.getOrgId() == null){
            user.setOrgId(Long.valueOf(getOrgId()));
        }
        startPage();
        List<SysUserEntity> list = userService.selectList(user);
        return AjaxResult.success(list,new PageInfo<>(list).getTotal());
    }

    @PostMapping("/changeStatus")
    public AjaxResult<Void> changeStatus(@RequestBody SysUserEntity user){
        user.setUpdateBy(getUserId());
        userService.changeStatus(user);
        return AjaxResult.success();
    }

    @GetMapping("/{userId}")
    public AjaxResult<SysUserEntity> getById(@PathVariable String userId){
        SysUserEntity sysUser = userService.selectUserById(userId);
        sysUser.setRoleIds(roleService.selectUserRoleIds(userId));
        return AjaxResult.success(sysUser);
    }

    @PostMapping("/saveUser")
    public AjaxResult<Void> saveUser(@RequestBody @Validated SysUserEntity user){
        String userId = getUserId();
        user.setCreateBy(userId);
        user.setOrgId(Long.valueOf(getOrgId()));
        user.setUserId(IdUtil.randomUUID());
        userService.saveUser(user);
        return AjaxResult.success();
    }


    @PostMapping("/updateUser")
    public AjaxResult<SysUserEntity> updateUser(@RequestBody @Validated SysUserEntity user){
        user.setUpdateBy(getUserId());
        userService.updateUser(user);
        roleService.updateUserRole(user);
        return AjaxResult.success(user);
    }


    @DeleteMapping("/{userId}")
    public AjaxResult<Void> deleteUser(@PathVariable String[] userId){
        userService.deleteUser(userId);
        return AjaxResult.success();
    }

    @PostMapping("/resetPwd")
    public AjaxResult<Void> resetPwd(@RequestBody SysUserEntity user){
        user.setUpdateBy(getUserId());
        userService.resetPwd(user);
        return AjaxResult.success();
    }
}
